<template>
    <div class="app-container">
        <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="单位名称" prop="unitName">
        <el-input
          v-model="queryParams.unitName"
          placeholder="请输入单位"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['work:unit:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['areas:remove']"
            >批量删除</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
  
      <el-table
        v-loading="loading"
        :data="roleList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" prop="id" width="120" />
  
        <el-table-column
          label="单位"
          align="center"
          prop="unitName"
          :show-overflow-tooltip="true"
          width="150"
        />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope" v-if="scope.row.roleId !== 1">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['areas:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['areas:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改角色配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="单位" prop="unitName">
            <el-input v-model="form.unitName" placeholder="请输入单位" />
          </el-form-item>
        </el-form>
  
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import {
    getUnitList,
    UnitInfo,
    updateUnit,
    delUnit,
    addUnit,
  } from "@/api/zhao/unit.js";
  import upimg from "../../components/UploadImg/index.vue";
  export default {
    components: {
      upimg,
    },
    name: "Role",
    dicts: ["sys_normal_disable"],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: "",
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 角色表格数据
        roleList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示弹出层（数据权限）
        openDataScope: false,
        menuExpand: false,
        menuNodeAll: false,
        deptExpand: true,
        deptNodeAll: false,
        // 日期范围
        dateRange: [],
        // 数据范围选项
        dataScopeOptions: [
          {
            value: "1",
            label: "全部数据权限",
          },
          {
            value: "2",
            label: "自定数据权限",
          },
          {
            value: "3",
            label: "本部门数据权限",
          },
          {
            value: "4",
            label: "本部门及以下数据权限",
          },
          {
            value: "5",
            label: "仅本人数据权限",
          },
        ],
        // 菜单列表
        menuOptions: [],
        // 部门列表
        deptOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          unitName: undefined,
        },
        // 表单参数
        form: {},
        defaultProps: {
          children: "children",
          label: "label",
        },
        // 表单校验
        rules: {
            unitName: [
            { required: true, message: "单位不能为空", trigger: "blur" },
          ],
        },
      };
    },
    created() {
      this.getList();
    },
    methods: {
      // 表单重置
      reset() {
        this.form = {
          img: '',
          remark:''
        };
        this.resetForm("form");
      },
      // 接受组件传递过来的值 也就是上传完成接口的返回
      imgUrlfun(e) {
        if (e) {
          this.form.img = e.url;
        } else {
          this.form.img = "";
        }
      },
      getList() {
        this.loading = true;
        getUnitList(this.queryParams).then((res) => {
          console.log(res);
          this.roleList = res.rows;
          this.total = res.total;
          this.loading = false;
        });
      },
  
      resetQuery() {
        this.queryParams.unitName = "";
        this.resetForm("queryForm");
      },
      handleUpdate(row) {
        const postId = row.id || this.ids;
        UnitInfo({ id: postId }).then((res) => {
          this.form = res.data;
          this.open = true;
        });
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.id);
        console.log(this.ids);
        this.multiple = !selection.length;
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        let postIds = "";
        if (row.id) {
          postIds = [row.id];
        } else {
          postIds = this.ids;
        }
        this.$modal
          .confirm('是否确认删除ID编号为"' + postIds + '"的数据项？')
          .then(function () {
            delUnit({ ids: postIds }).then((res) => {
              console.log(res);
            });
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        console.log(this.form);
        this.open = true;
        this.title = "添加轮播图";
      },
      handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams.unitName = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.id != undefined) {
                updateUnit(this.form).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
                addUnit(this.form).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
    },
  };
  </script>
  <style>
  .img {
    width: 120px;
    height: 60px;
  }
  </style>
  